//
//  LYMineTeacherViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/6.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineTeacherViewController.h"
#import "LYTeacherCell.h"
#import "LYTeacherWebViewController.h"

@interface LYMineTeacherViewController ()<VVYRefreshTableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *models;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, assign) NSInteger page;

@end

@implementation LYMineTeacherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self.tableView.mj_header beginRefreshing];
    
}

#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    
    [LYRequestTool getMyTeacherWithToken:LYToken page:@"1" per_page:@"10" success:^(NSArray<LYPersonalInfo *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;

        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    NSString *perpage = @"10";
    
    [LYRequestTool getMyTeacherWithToken:LYToken page:page per_page:perpage success:^(NSArray<LYPersonalInfo *> *models) {
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_footer endRefreshing];
    }];
    

    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}


#pragma mark - tableviewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYTeacherCell";
    
    LYTeacherCell *cell = [LYTeacherCell cellWithTableView:tableView andIndetifier:cellID];
    
    LYPersonalInfo *model = self.models[indexPath.section];
    cell.model = model;
    
    return cell;
}

#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.1;
    }else{
        return 5;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYPersonalInfo *info = self.models[indexPath.section];
    LYTeacherWebViewController *teaWebVC = [LYTeacherWebViewController teaWebVCWithID:info.user_id];
    [self.navigationController pushViewController:teaWebVC animated:YES];
    
}
@end
